﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization.Json;
using Croxweb.Leuphana.Models;

namespace Croxweb.Leuphana.Services.StorageService
{
    /// <summary>
    /// Stores lecture information in the isolated storage.
    /// </summary>
    public class PhoneScheduleStore : IScheduleStore
    {
		/// <summary>
        /// Creates a new instance of Croxweb.Leuphana.Services.StorageService.ScheduleStore,
        /// that accesses lectures stored in an isolated storage file.
		/// </summary>
        public PhoneScheduleStore() { }


        /// <summary>
        /// <see cref="Croxweb.Leuphana.Services.StorageService.IScheduleStore.GetLectures"/>
        /// </summary>
        /// <returns>List of lectures.</returns>
        public IEnumerable<Lecture> GetLectures()
        {
            String filename = "schedule";
            List<Lecture> lectures;

            // Open critical section.
            lock (this)
            {
                // Get application specific isolated storage file.
                using (var filesystem = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // If no store has been created yet. 
                    if (!filesystem.FileExists(filename))
                    {
                        lectures = new List<Lecture>();
                    }
                    else
                    {
                        
                        using (var stream = new IsolatedStorageFileStream(filename, System.IO.FileMode.Open, filesystem))
                        {
                            var serializer = new DataContractJsonSerializer(typeof(List<Lecture>));
                            lectures = serializer.ReadObject(stream) as List<Lecture>;
                        }
                    }
                }
            }

            return lectures;
        }


        /// <summary>
        /// <see cref="Croxweb.Leuphana.Services.StorageService.IScheduleStore.SaveLectures"/>
        /// </summary>
        /// <param name="lectures">List of Lectures to store.</param>
        public void SaveLectures(IEnumerable<Lecture> lectures)
        {
            String filename = "schedule";
            // Open critical section.
			lock (this)
			{
                // Get application specific isolated storage file.
				using (var filesystem = IsolatedStorageFile.GetUserStoreForApplication())
				{
                    // Serialize lectures to Json.
                    using (var stream = new IsolatedStorageFileStream(filename, System.IO.FileMode.Create, filesystem))
					{
						var serializer = new DataContractJsonSerializer(typeof(List<Lecture>));
						serializer.WriteObject(stream, lectures);
					}
				}
			}
        }
    

        /// <summary>
        /// <see cref=" Croxweb.Leuphana.Services.StorageService.IScheduleStore.GetMessages"/>
        /// </summary>
        /// <returns>List of messages.</returns>
        public IEnumerable<Message> GetMessages()
        {
 	        String filename = "messages";
            List<Message> messages;

            // Open critical section.
            lock (this)
            {
                // Get application specific isolated storage file.
                using (var filesystem = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // If no store has been created yet.
                    if (!filesystem.FileExists(filename))
                    {
                        messages = new List<Message>();
                    }
                    else
                    {
                        // Deserialize from Json.
                        using (var stream = new IsolatedStorageFileStream(filename, System.IO.FileMode.Open, filesystem))
                        {
                            var serializer = new DataContractJsonSerializer(typeof(List<Message>));
                            messages = serializer.ReadObject(stream) as List<Message>;
                        }
                    }
                }
            }

            return messages;
        }


        /// <summary>
        /// <see cref="Croxweb.Leuphana.Services.StorageService.IScheduleStore.SaveMessages"/>
        /// </summary>
        /// <param name="messages">List of messages to store.</param>
        public void  SaveMessages(IEnumerable<Message> messages)
        {
            String filename = "messages";
            // Open critical section.
            lock (this)
            {
                // Get application specific isolated storage file.
                using (var filesystem = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // Serialize lectures to Json.
                    using (var stream = new IsolatedStorageFileStream(filename, System.IO.FileMode.Create, filesystem))
                    {
                        var serializer = new DataContractJsonSerializer(typeof(List<Message>));
                        serializer.WriteObject(stream, messages);
                    }
                }
            }
        }
        

        /// <summary>
        /// <see cref=" Croxweb.Leuphana.Services.StorageService.IScheduleStore.GetAppointments"/>
        /// </summary>
        /// <returns>List of appointments.</returns>
        public IEnumerable<Appointment> GetAppointments()
        {
            String filename = "appointments";
            List<Appointment> appointments;

            // Open critical section.
            lock (this)
            {
                // Get application specific isolated storage file.
                using (var filesystem = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // If no store has been created yet.
                    if (!filesystem.FileExists(filename))
                    {
                        appointments = new List<Appointment>();
                    }
                    else
                    {
                        // Deserialize from Json.
                        using (var stream = new IsolatedStorageFileStream(filename, System.IO.FileMode.Open, filesystem))
                        {
                            var serializer = new DataContractJsonSerializer(typeof(List<Appointment>));
                            appointments = serializer.ReadObject(stream) as List<Appointment>;
                        }
                    }
                }
            }

            return appointments;
        }


        /// <summary>
        /// <see cref="Croxweb.Leuphana.Services.StorageService.IScheduleStore.SaveAppointments"/>
        /// </summary>
        /// <param name="messages">List of appointments to store.</param>
        public void SaveAppointments(IEnumerable<Appointment> appointments)
        {
            String filename = "appointments";
            // Open critical section.
            lock (this)
            {
                // Get application specific isolated storage file.
                using (var filesystem = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // Serialize lectures to Json.
                    using (var stream = new IsolatedStorageFileStream(filename, System.IO.FileMode.Create, filesystem))
                    {
                        var serializer = new DataContractJsonSerializer(typeof(List<Appointment>));
                        serializer.WriteObject(stream, appointments);
                    }
                }
            }
        }
    }
}
